/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {Text, View, Image, TouchableHighlight, NativeModules, Platform, ListView} from 'react-native';

import Common from '../../component/common';

import Loading from '../../component/loading';

import stylesm from '../../style/media';

import Article from './article';

const wechat = require('../../images/wechat.png');
const weibo = require('../../images/weibo.png');
const web = require('../../images/newspaper.png');
// const like = require('../../images/home/like.png');
// const dislike = require('../../images/home/dislike.png');

export default class AnalysisList extends Component {
  constructor(props) {
    super(props);
    var dsindustry = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
    this.state = {
      loading: false,
      isMore: true,
      analysisDataSource: dsindustry,
    };
    this.analysis = [];
    this.analysismore = [];
    this.analysistotal = [];
  }
  componentWillMount(){
    if(this.props.posNegData.length){
      this.analysis = this.props.posNegData;
      this.setState({
        analysisDataSource: this.state.analysisDataSource.cloneWithRows(this.analysis),
      });
    }else{
      this.analysis = [];
      this.setState({
        analysisDataSource: this.state.analysisDataSource.cloneWithRows(this.analysis),
      });
    }
  }
  // analysisMore = () => {
  //   this.setState({loading: true});
  //   this.timer = setTimeout(() => {
  //     if(!this.analysismore.length){
  //       Common.fetchMore({id: this.props.data.newsGroupId || this.props.data.programId, type: this.props.data.type,newsType: 3},(responseData) => {
  //         // console.log(responseData.resultList);
  //         this.analysismore = responseData.resultList;
  //         this.analysistotal = this.analysis.concat(this.analysismore);
  //         this.setState({
  //           loading: false,
  //           isMore: false,
  //           analysisDataSource: this.state.analysisDataSource.cloneWithRows(this.analysistotal)});
  //       },() => {
  //         console.log("error");
  //       }, () => {
  //         console.log("error link");
  //       });
  //     }else{
  //       this.analysistotal = this.analysis.concat(this.analysismore);
  //       this.setState({
  //           loading: false,
  //           isMore: false,
  //           analysisDataSource: this.state.analysisDataSource.cloneWithRows(this.analysistotal)});
  //     }
  //     clearTimeout(this.timer);
  //   }, 500);
  // };
  // analysisRestore = () => {
  //   this.setState({
  //     isMore: true,
  //     analysisDataSource: this.state.analysisDataSource.cloneWithRows(this.analysis)});
  // };
  linking = (val) => {
    if(Platform.OS == 'ios'){
      NativeModules.JsAndroid.openUrl(val.url);
    }else{
      Common.onForward(this, Article, val, 5);
    }
  }
  renderRow = (val) => {
    let sourceImage, negPos;
    switch(val.scrabbleType){
      case 'wechat':
        sourceImage = wechat;
        break;
      case 'web':
        sourceImage = web;
        break;
      default :
        sourceImage = weibo;
        break;
    }
    // if(this.props.isPosNeg){
    //   negPos = like;
    // }else{
    //   negPos = dislike;
    // }
    return (
      <TouchableHighlight style={stylesm.reportListItems} underlayColor="#fafafa" delayPressIn={100} onPress={() => {this.linking(val)}}>
        <View>
          <View style={stylesm.reportListIW}>
            {/*<Image style={stylesm.reportListII} source={negPos} />*/}
            <Text style={stylesm.reportListIT} numberOfLines={1}>{val.title}</Text>
          </View>
          <View style={stylesm.reportLIs}>
            <View style={stylesm.reportLIsr}>
              <Image style={stylesm.reportLIsri} source={sourceImage} />
              <Text style={stylesm.reportLIsrl}>{val.company}</Text>
            </View>
            <View style={stylesm.reportLIsr}>
              <Image style={stylesm.reportLIsri} source={require('../../images/timer.png')} />
              <Text style={stylesm.reportLIsrl}>
                {
                  Common.beforeTime(val.createTime,5)
                }
              </Text>
            </View>
            {/*<View style={stylesm.reportLIsr}>
              <Text style={stylesm.reportLIsrc}>{val.sourceDegree}</Text>
              <Text style={stylesm.reportLIsrl}>阅读</Text>
            </View>*/}
          </View>
        </View>
      </TouchableHighlight>
    )
  };
  render() {
    return (
      <View>
        {this.analysis?
          <ListView
            scrollEnabled={true}
            removeClippedSubviews={false}
            dataSource={this.state.analysisDataSource}
            renderRow={this.renderRow}
            enableEmptySections={true}
            style={{borderBottomLeftRadius: 10,borderBottomRightRadius: 10}}
          />:null
        }
      </View>
    )
  }
}